<?php
/**
 * 视图静态类
 *
 * @author will
 * @version $Id$
 */

class View
{
	/**
	 * 统一成功页面
	 * @param <string> $msg
	 * @param <array> $urls
	 * @return <string>
	 */
	static public function success($msg , $urls = array()) {
		// 如果有post的 back_url，则 自动设置变量
		if(!empty($_POST['back_url'])) {
			// 这里需要对 post 数据做过滤  ..todo
			$urls[] = array(
				'name' => empty($_POST['back_url_name']) ? '返回上一页' : $_POST['back_url_name'],
				'url'  => str_replace('&' , '&amp;' , $_POST['back_url']),
			);
		}
		
		$smarty = loadSmarty();
		$smarty->assign('msg' , $msg);
		$smarty->assign('urls' , $urls);
		
		return $smarty->fetch('common/success.tpl.php');
	}
	
	/**
	 * 统一错误页面
	 * @param <string> $msg
	 * @param <int> $code 0为默认，无意义。小于0为正常逻辑错误。大于0为系统错误
	 * @return <string>
	 */
	static public function error($msg , $code = 0 ,$urls = array()) {
		if($code > 0) {
			// 上报系统错误
			// ..todo
		}
		
		$smarty = loadSmarty();
		$smarty->assign('msg' , $msg);
		$smarty->assign('code' , $code);
		$smarty->assign('urls' , $urls);
		
		return $smarty->fetch('common/error.tpl.php');
	}
	
	/**
	 * 统一跳转页面
	 * @param <string> $msg
	 * @param <array> $urls
	 * @return <string>
	 */
	static public function redirect($msg , $time = 3) {
		$smarty = loadSmarty();
		$smarty->assign('msg' , $msg);
		$smarty->assign('time' , $time);
		
		return $smarty->fetch('common/redirect.tpl.php');
	}
	
	/**
	 * 准备个人主页导航数据
	 * @param <int> $uin
	 */
	static public function prepare_profile_nav_data($uin) {
		// 获取用户数据
		loadLib('User');
		$userinfo = USER::getBaseInfo($uin);
		
		$smarty = loadSmarty();
		$smarty->assign('userinfo' , $userinfo);
	}
	
	/**
	 * 通用翻页展示
	 * @param <int> $total
	 * @param <int> $per
	 * @param <int> $midNum 
	 */
	static public function pager($total = 0 , $per = 2 , $mid_num = 7 , $path = false) {
		$page_count = ceil($total / $per);
		
		// 如果只有一页，不展示页码
		if($page_count <= 1) {
			return '';
		}
		
		$cur_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		if($cur_page < 1) {
			$cur_page = 1;
		} elseif($cur_page > $page_count) {
			$cur_page = $page_count;
		}
	
		$next_page = $cur_page + 1;
		$prev_page = $cur_page - 1;
	
		// 下面是一个纯粹误会早期所写的复杂语句，功能：让当前页永远距中，并使分页链接数量保持平衡
		$current_page = $cur_page;
		$offset = floor($mid_num / 2);
		if($current_page - $offset <= 0) $current_page = $offset + 1;
		if($current_page + $offset > $page_count) $current_page = $page_count - $offset;
		$begin = ($current_page - $offset) > 0 ? $current_page - $offset : 1;
		$end = ($current_page + $offset) < $page_count ? $current_page + $offset : $page_count;
	
		// 获取地址
		$url_prefix = 'http://' . $_SERVER['HTTP_HOST'] . preg_replace('%/index\.php$%' , '/' , $_SERVER['SCRIPT_NAME']) . '?';
		$url_params = array();
		if(!empty($_GET)) {
			foreach($_GET as $key => $value) {
				if($key == 'page') continue;
				$url_params[] = urlencode($key) . '=' . urlencode($value);
			}
		}
		$url_params[] = 'page=';
		$url_prefix = $url_prefix . implode('&amp;' , $url_params);
		
		$page_str = '';
		
		if($cur_page > 1) {
			$page_str .= '<a href="' . $url_prefix . $prev_page . '">上一页</a>';
		}
		
		for($i = $begin ; $i <= $end ; $i++) {
			$page_str .= $cur_page == $i
				? ' <strong>' . $i . '</strong> '
				: ' <a href="' . $url_prefix . $i . '">' . $i . '</a> ';
		}
		
		if($cur_page < $page_count) {
			$page_str .= ' <a href="' . $url_prefix . $next_page . '">下一页</a>';
		}

		return $page_str;
	}
}


# end of script

